/*
 Xern Entity Relocation Network
 Copyright (C) 2011 Ronie Salgado <roniesalg@gmail.com>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

#ifndef XERNP_CLIENT_SESSION_HPP
#define XERNP_CLIENT_SESSION_HPP

#include <boost/enable_shared_from_this.hpp>
#include "Xern/IResourceListener.hpp"
#include "Xern/IResource.hpp"
#include "Session.hpp"
#include "Message.hpp"
#include "SessionMessage.hpp"
#include "EntityMessages.hpp"
#include "PlayerMessages.hpp"
#include "RegionMessages.hpp"
#include "ResourceMessages.hpp"
#include "TransferManager.hpp"

namespace XernProtocol
{
	class RemoteRegion;
	class RemotePlayer;
	class EntityAddResourceCallback;
	class EntityLoadedShapeCallback;
	class ClientSession: public IResourceListener,
			public boost::enable_shared_from_this<ClientSession>
	{
	public:
		enum ClientState
		{
			CS_INITIATING = 0,
			CS_WAIT_REGION,
			CS_WAIT_PLAYER,
			CS_RUNNING,
		};

		static const posix_time::ptime TimeEpoch;
		static const posix_time::time_duration CheckPingInterval;

		ClientSession(boost::shared_ptr<Session> session);
		~ClientSession();

		void Opened();
		void Closed();

		void OnAddedResource(boost::shared_ptr<Entity> entity, boost::shared_ptr<IResource> resource);
		void OnRemovedResource(boost::shared_ptr<Entity> entity, boost::shared_ptr<IResource> resource);
		void OnUpdateState(boost::shared_ptr<IResource> resource, boost::shared_ptr<IDataFilter> stateData);
		bool OnDelayedLoad(const uuid &id, boost::shared_ptr<IResourceLoadedCallback> callback);

		void Think(float delta);
		boost::shared_ptr<Entity> CreatePlayerController(boost::shared_ptr<Region> region);

		void OnFinishAdd(boost::shared_ptr<EntityAddResourceCallback> add);
		void OnFinishPhysicUpdate(boost::shared_ptr<EntityLoadedShapeCallback> add);

	private:
		typedef std::pair<uuid, uuid> ResourceBinding; // first:entity, second:resource.
		typedef std::multimap<uuid, boost::shared_ptr<IResource> > DelayedResources;
		typedef std::map<ResourceBinding, boost::shared_ptr<EntityAddResourceCallback> > AddResourcesCallbacks;

		typedef std::map<uuid, boost::shared_ptr<EntityLoadedShapeCallback> > UpdatePhysicsCallbacks;

		posix_time::ptime TransformTime(u64 sentTime) const;
		float CalculateDelta(u64 sentTime) const;

		void CheckPing();
		void OnSessionPingRequest(boost::shared_ptr<SessionPingRequest> request);
		void OnSessionPingResponse(boost::shared_ptr<SessionPingResponse> response);

		void OnEntityAdded(boost::shared_ptr<EntityAdded> added);
		void OnEntityRemoved(boost::shared_ptr<EntityRemoved> removed);
		void OnEntityMovementUpdates(boost::shared_ptr<EntityMovementUpdates> updates);
		void OnEntityPhysicUpdates(boost::shared_ptr<EntityPhysicUpdates> updates);
		void OnEntityAddedResource(boost::shared_ptr<EntityAddedResource> message);
		void OnEntityRemovedResource(boost::shared_ptr<EntityRemovedResource> message);
		void OnResourceState(boost::shared_ptr<ResourceState> message);
		void OnPlayerEntityResponse(boost::shared_ptr<PlayerEntityResponse> response);
		void OnRegionListResponse(boost::shared_ptr<RegionListResponse> list);
		void RequestRegion();

		boost::shared_ptr<Session> session;
		boost::shared_ptr<RemoteRegion> remoteRegion;
		boost::shared_ptr<RemotePlayer> remotePlayer;

		DelayedResources delayedResources;
		AddResourcesCallbacks addResourcesCallbacks;

		UpdatePhysicsCallbacks updatePhysicsCallback;

		ClientState state;
		TransferManager transferManager;

		u32 lastPingId;
		posix_time::ptime lastPingTime;
		posix_time::time_duration serverTimeDelta;
		u64 currentPing;
	};

}; // namespace XernProtocol

#endif //XERNP_CLIENT_SESSION_HPP
